##Intragroup_Conflict_Leadership_Study_2
##Judges study, hypothetical state governor

##Load data
setwd("C:/Users/lmh735/Dropbox (Political Science)/Partisanship and leadership-Trump/2021 POBE Submission/R&R/Replication Files")
data2.finish<- read.csv("Study_2.csv", header=TRUE)

##Create data for analyzing partisans pooled, dropping Independents
data2.partisans<- subset(data2.finish, data2.finish$OwnParty!="Independent")

###############################
##Appendix Table E2
##Summary stats and balance tables (partisans only) 
##Balance using chi-squared, exclude independents since excluded in analysis
library(MASS) ##(only run for this, causes issues with tdyr/dplyr, need to detach after)
##gender
balance.gender<- table(data2.partisans$female, data2.partisans$full.treatment.pool.party)
prop.table(balance.gender, 2)
chisq.test(balance.gender)
##education
balance.educ<- table(data2.partisans$educ, data2.partisans$full.treatment.pool.party)
prop.table(balance.educ, 2)
chisq.test(balance.educ)
##income
balance.income<- table(data2.partisans$income, data2.partisans$full.treatment.pool.party)
prop.table(balance.income, 2)
chisq.test(balance.income)
##age (categorical)
balance.age.cat<- table(data2.partisans$age.cat, data2.partisans$full.treatment.pool.party)
prop.table(balance.age.cat, 2)
chisq.test(balance.age.cat)

##sample demographics
##gender
sample.gender<- table(data2.partisans$female)
prop.table(sample.gender)
##education
sample.educ<- table(data2.partisans$educ)
prop.table(sample.educ)
##income
sample.income<- table(data2.partisans$income)
prop.table(sample.income)
##age (categorical)
sample.age.cat<- table(data2.partisans$age.cat)
prop.table(sample.age.cat)

detach("package:MASS", unload=TRUE)
detach("package:psych", unload=TRUE)

#########################################
##Appendix Table E3
##Summary Stats (post-dissent measures)
summary(data2.partisans$ft_leader2)
length(na.omit(data2.partisans$ft_leader2))
sd(data2.partisans$ft_leader2, na.rm=TRUE)

summary(data2.partisans$voteL2)
length(na.omit(data2.partisans$voteL2))
sd(data2.partisans$voteL2, na.rm=TRUE)

summary(data2.partisans$ft_critic)
length(na.omit(data2.partisans$ft_critic))
sd(data2.partisans$ft_critic, na.rm=TRUE)

summary(data2.partisans$appropriateC)
length(na.omit(data2.partisans$appropriateC))
sd(data2.partisans$appropriateC, na.rm=TRUE)

summary(data2.partisans$vote_crit)
length(na.omit(data2.partisans$vote_crit))
sd(data2.partisans$vote_crit, na.rm=TRUE)

###############################
##Appendix Table E1
##N by condition
library(tidyverse)

study2.N<- data2.finish %>%
  mutate(party=factor(OwnParty, levels=c("Independent", "Democrat", "Republican"), labels=c("Ind", "Dem", "GOP"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(party, ResponseCondition.new, Violation) %>%
  gather("measure", "value", -party, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  filter(party!="Ind") %>%
  group_by(ResponseCondition.new, party) %>%
  summarize(n=n())

study2.N
sum(study2.N$n)


#####################################
##Load packages for analyses
##Analysis with dplyr
library(dplyr)
library(tidyr)
library(infer)
library(ggplot2)
library(gridExtra)

##for regression tables
source("regtable.R")

##set directory for output
setwd("C:/Users/lmh735/Dropbox (Political Science)/Partisanship and leadership-Trump/2021 POBE Submission/R&R/Replication Files/Output")


##leader affect, post response
study2.resp.all.ft_leader2<- data2.finish %>%
  mutate(party=factor(OwnParty, levels=c("Independent", "Democrat", "Republican"), labels=c("Ind", "Dem", "GOP"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(party, ResponseCondition.new, ft_leader2) %>%
  gather("measure", "value", -party, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  filter(party!="Ind") %>%
  group_by(ResponseCondition.new) %>%
  summarize(mean=mean(value),
            sd=sd(value),
            n=n(),
            se=sd/sqrt(n))

study2.resp.all.ft_leader2

##plot
judges_ld_ft<- ggplot(study2.resp.all.ft_leader2) + # define aes for each individual component instead
  geom_errorbar(aes(x=ResponseCondition.new, ymin=mean-1.96*se, ymax=mean+1.96*se), ##95% CI
                width=.15) +
  geom_point(aes(x=ResponseCondition.new, y=mean), size=2) + # add the points on
  scale_y_continuous(limits=c(0,1)) + 
  theme(axis.text=element_text(size=12), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Governor Feeling Thermometer") +
  ggtitle("(a) Leader Feeling Thermometer")

##t-test
t.test(data2.partisans$ft_leader2 ~ data2.partisans$ResponseCondition)

##partisan differences
party_reg_ft_leader2<- lm(ft_leader2 ~ ResponseCondition*OwnParty,
                    data=data2.partisans)
summary(party_reg_ft_leader2)

###########################################
##leader vote, post response
study2.resp.all.voteL2<- data2.finish %>%
  mutate(party=factor(OwnParty, levels=c("Independent", "Democrat", "Republican"), labels=c("Ind", "Dem", "GOP"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(party, ResponseCondition.new, voteL2) %>%
  gather("measure", "value", -party, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  filter(party!="Ind") %>%
  group_by(ResponseCondition.new) %>%
  summarize(mean=mean(value),
            sd=sd(value),
            n=n(),
            se=sd/sqrt(n))

study2.resp.all.voteL2

##plot
judges_ld_vote<- ggplot(study2.resp.all.voteL2) + # define aes for each individual component instead
  geom_errorbar(aes(x=ResponseCondition.new, ymin=mean-1.96*se, ymax=mean+1.96*se), ##95% CI
                width=.15) +
  geom_point(aes(x=ResponseCondition.new, y=mean), size=2) + # add the points on
  scale_y_continuous(limits=c(0,1)) + 
  theme(axis.text=element_text(size=12), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Governor Vote") +
  ggtitle("(b) Leader Vote")

##t-test
t.test(data2.partisans$voteL2 ~ data2.partisans$ResponseCondition)

##partisan differences
party_reg_voteL2<- lm(voteL2 ~ ResponseCondition*OwnParty,
                    data=data2.partisans)
summary(party_reg_voteL2)


###########################################
##critic affect, post response
study2.resp.all.ft_critic<- data2.finish %>%
  mutate(party=factor(OwnParty, levels=c("Independent", "Democrat", "Republican"), labels=c("Ind", "Dem", "GOP"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(party, ResponseCondition.new, ft_critic) %>%
  gather("measure", "value", -party, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  filter(party!="Ind") %>%
  group_by(ResponseCondition.new) %>%
  summarize(mean=mean(value),
            sd=sd(value),
            n=n(),
            se=sd/sqrt(n))

study2.resp.all.ft_critic

##plot
judges_al_ft<- ggplot(study2.resp.all.ft_critic) + # define aes for each individual component instead
  geom_errorbar(aes(x=ResponseCondition.new, ymin=mean-1.96*se, ymax=mean+1.96*se), ##95% CI
                width=.15) +
  geom_point(aes(x=ResponseCondition.new, y=mean), size=2) + # add the points on
  scale_y_continuous(limits=c(0,1)) + 
  theme(axis.text=element_text(size=12), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Assembly Leader Feeling Thermometer") +
  ggtitle("(c) Assembly Leader Feeling Thermometer")

##t-test
t.test(data2.partisans$ft_critic ~ data2.partisans$ResponseCondition)

##partisan differences
party_reg_ft_critic<- lm(ft_critic ~ ResponseCondition*OwnParty,
                    data=data2.partisans)
summary(party_reg_ft_critic)

###########################################
##critic vote, post response
study2.resp.all.vote_critic<- data2.finish %>%
  mutate(party=factor(OwnParty, levels=c("Independent", "Democrat", "Republican"), labels=c("Ind", "Dem", "GOP"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(party, ResponseCondition.new, vote_critic) %>%
  gather("measure", "value", -party, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  filter(party!="Ind") %>%
  group_by(ResponseCondition.new) %>%
  summarize(mean=mean(value),
            sd=sd(value),
            n=n(),
            se=sd/sqrt(n))

study2.resp.all.vote_critic

##plot
judges_al_vote<- ggplot(study2.resp.all.vote_critic) + # define aes for each individual component instead
  geom_errorbar(aes(x=ResponseCondition.new, ymin=mean-1.96*se, ymax=mean+1.96*se), ##95% CI
                width=.15) +
  geom_point(aes(x=ResponseCondition.new, y=mean), size=2) + # add the points on
  scale_y_continuous(limits=c(0,1)) + 
  theme(axis.text=element_text(size=12), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Assembly Leader Vote") +
  ggtitle("(d) Assembly Leader Vote")

##t-test
t.test(data2.partisans$vote_critic ~ data2.partisans$ResponseCondition)

##partisan differences
party_reg_vote_critic<- lm(vote_critic ~ ResponseCondition*OwnParty,
                    data=data2.partisans)
summary(party_reg_vote_critic)


#############################################
##Output figures for each study
blank<- ggplot() + theme_void()

##Hypothetical Pardon
grid.arrange(judges_ld_ft, judges_ld_vote, judges_al_ft, judges_al_vote, nrow=2)
fig.pardon.g<- arrangeGrob(judges_ld_ft, judges_ld_vote, judges_al_ft, judges_al_vote, nrow=2)
ggsave("Fig5_hyp_judges_all_DV.png", fig.pardon.g, height=10, width=12)


###########################################
##Mechanisms

##appropriateness of response, post response - mechanisms
##study 2
study2.resp.all.appropriateC<- data2.finish %>%
  mutate(party=factor(OwnParty, levels=c("Independent", "Democrat", "Republican"), labels=c("Ind", "Dem", "GOP"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(party, ResponseCondition.new, appropriateC) %>%
  gather("measure", "value", -party, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  filter(party!="Ind") %>%
  group_by(ResponseCondition.new) %>%
  summarize(mean=mean(value),
            sd=sd(value),
            n=n(),
            se=sd/sqrt(n))

study2.resp.all.appropriateC

##plot
judges_approp<- ggplot(study2.resp.all.appropriateC) + # define aes for each individual component instead
  geom_errorbar(aes(x=ResponseCondition.new, ymin=mean-1.96*se, ymax=mean+1.96*se), ##95% CI
                width=.15) +
  geom_point(aes(x=ResponseCondition.new, y=mean), size=2) + # add the points on
  scale_y_continuous(limits=c(0,1)) + 
  theme(axis.text=element_text(size=12), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Appropriateness of Response") +
  ggtitle("(a) Appropropriateness of Assembly Leader Response")

##t-test
t.test(data2.partisans$appropriateC ~ data2.partisans$ResponseCondition)

##partisan differences
party_reg_appropriateC<- lm(appropriateC ~ ResponseCondition*OwnParty,
                    data=data2.partisans)
summary(party_reg_appropriateC)


########################################
##who best serves the party histogram
##plot distribution (N) by condition:
study2.resp.all.best.serve.party<- data2.finish %>%
  mutate(party=factor(OwnParty, levels=c("Independent", "Democrat", "Republican"), labels=c("Ind", "Dem", "GOP"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(party, ResponseCondition.new, best.serve.party.cat) %>%
  gather("measure", "value", -party, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  filter(party!="Ind") %>%
  group_by(ResponseCondition.new) 

study2.resp.all.best.serve.party
##set order
study2.resp.all.best.serve.party$value<- factor(study2.resp.all.best.serve.party$value, levels=c("Leader", "Assembly", "Both", "Neither"), ordered=FALSE)

##plot (https://socviz.co/groupfacettx.html)
judges_best_serves<- ggplot(study2.resp.all.best.serve.party, mapping=(aes(x=value))) + # define aes for each individual component instead
  geom_bar(mapping = aes(y = ..prop.., group = ResponseCondition.new))  + 
  facet_grid(~ ResponseCondition.new) + # split into plots, side by side
  scale_y_continuous(breaks=c(0,.25, .5, .75, 1), limits=c(0,1)) + 
  theme(axis.text=element_text(size=12, angle=90), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Proportion") +
  ggtitle("(b) Who Best Serves the Party")
judges_best_serves

##calc proportion
study2.resp.all.best.serve.party.prop<- study2.resp.all.best.serve.party %>%
  count(ResponseCondition.new, value, name="n_response_best_serves")
study2.resp.all.best.serve.party.prop
#####loyalty condition
##Leader
study2.resp.all.best.serve.party.prop$n_response_best_serves[1]/sum(study2.resp.all.best.serve.party.prop$n_response_best_serves[1:4])
##Assembly Leader
study2.resp.all.best.serve.party.prop$n_response_best_serves[2]/sum(study2.resp.all.best.serve.party.prop$n_response_best_serves[1:4])
##Both
study2.resp.all.best.serve.party.prop$n_response_best_serves[3]/sum(study2.resp.all.best.serve.party.prop$n_response_best_serves[1:4])
##Neither
study2.resp.all.best.serve.party.prop$n_response_best_serves[4]/sum(study2.resp.all.best.serve.party.prop$n_response_best_serves[1:4])
######dissent condition
##Leader
study2.resp.all.best.serve.party.prop$n_response_best_serves[5]/sum(study2.resp.all.best.serve.party.prop$n_response_best_serves[5:8])
##assembly leader
study2.resp.all.best.serve.party.prop$n_response_best_serves[6]/sum(study2.resp.all.best.serve.party.prop$n_response_best_serves[5:8])
##both
study2.resp.all.best.serve.party.prop$n_response_best_serves[7]/sum(study2.resp.all.best.serve.party.prop$n_response_best_serves[5:8])
##Neither
study2.resp.all.best.serve.party.prop$n_response_best_serves[8]/sum(study2.resp.all.best.serve.party.prop$n_response_best_serves[5:8])


###########################################
##who embarrasses the party histogram
##plot distribution (N) by condition:
study2.resp.all.embarrass.party<- data2.finish %>%
  mutate(party=factor(OwnParty, levels=c("Independent", "Democrat", "Republican"), labels=c("Ind", "Dem", "GOP"), ordered=FALSE)) %>%
  mutate(ResponseCondition.new=factor(ResponseCondition, levels=c("Agree", "Rebuke"), labels=c("Loyalist", "Dissent"), ordered=FALSE)) %>%
  select(party, ResponseCondition.new, embarrass.party.cat) %>%
  gather("measure", "value", -party, -ResponseCondition.new) %>%
  filter(!is.na(value)) %>%
  filter(party!="Ind") %>%
  group_by(ResponseCondition.new) 

study2.resp.all.embarrass.party
##set order
study2.resp.all.embarrass.party$value<- factor(study2.resp.all.embarrass.party$value, levels=c("Leader", "Assembly", "Both", "Neither"), ordered=FALSE)

##plot (https://socviz.co/groupfacettx.html)
judges_embarrass<- ggplot(study2.resp.all.embarrass.party, mapping=(aes(x=value))) + # define aes for each individual component instead
  geom_bar(mapping = aes(y = ..prop.., group = ResponseCondition.new))  + 
  facet_grid(~ ResponseCondition.new) + # split into plots, side by side
  scale_y_continuous(breaks=c(0,.25, .5, .75, 1), limits=c(0,1)) + 
  theme(axis.text=element_text(size=12, angle=90), axis.title.y = element_text(size = 12),
        plot.title = element_text(size=12))+
  theme(axis.ticks.x=element_blank(), axis.title.x=element_blank(),
        #axis.title.y=element_blank(),
        strip.text.x = element_text(size=10)) + # how to change facet label font size
  ylab("Proportion") +
  ggtitle("(c) Who Embarrasses the Party")
judges_embarrass

##calc proportion
study2.resp.all.embarrass.party.prop<- study2.resp.all.embarrass.party %>%
  count(ResponseCondition.new, value, name="n_response_embarrass")
study2.resp.all.embarrass.party.prop
#####loyalty condition
##Leader
study2.resp.all.embarrass.party.prop$n_response_embarrass[1]/sum(study2.resp.all.embarrass.party.prop$n_response_embarrass[1:4])
##Assembly Leader
study2.resp.all.embarrass.party.prop$n_response_embarrass[2]/sum(study2.resp.all.embarrass.party.prop$n_response_embarrass[1:4])
##Both
study2.resp.all.embarrass.party.prop$n_response_embarrass[3]/sum(study2.resp.all.embarrass.party.prop$n_response_embarrass[1:4])
##Neither
study2.resp.all.embarrass.party.prop$n_response_embarrass[4]/sum(study2.resp.all.embarrass.party.prop$n_response_embarrass[1:4])
######dissent condition
##Leader
study2.resp.all.embarrass.party.prop$n_response_embarrass[5]/sum(study2.resp.all.embarrass.party.prop$n_response_embarrass[5:8])
##assembly leader
study2.resp.all.embarrass.party.prop$n_response_embarrass[6]/sum(study2.resp.all.embarrass.party.prop$n_response_embarrass[5:8])
##both
study2.resp.all.embarrass.party.prop$n_response_embarrass[7]/sum(study2.resp.all.embarrass.party.prop$n_response_embarrass[5:8])
##Neither
study2.resp.all.embarrass.party.prop$n_response_embarrass[8]/sum(study2.resp.all.embarrass.party.prop$n_response_embarrass[5:8])


###########################################
##Plot figures of mechanisms
##Pardon study
grid.arrange(judges_approp, judges_best_serves, judges_embarrass, nrow=1)
judges_mech<- arrangeGrob(judges_approp, judges_best_serves, judges_embarrass, nrow=1)
ggsave("Fig6_judges_mechanisms.png", judges_mech, height=5, width=15)

############################################
##Partisan Differences in Response
##Appendix Table C4

outtable.rtf(list("(1) Leader FT"= party_reg_ft_leader2, "(2) Leader Vote"= party_reg_voteL2,
                  "(3) Critic FT" = party_reg_ft_critic, "(4) Critic Vote"= party_reg_vote_critic,
                  "(5) Approp. Response" = party_reg_appropriateC),
             replacelist=list(c("(Intercept)", "Constant"),
                              c("ResponseConditionRebuke", "Dissent"),
                              c("OwnPartyRepublican", "Republican"),
                              c("ResponseConditionRebuke:OwnPartyRepublican", "Dissent x Rep")),
             p.levels =c(0.10,0.05,0.01,0.001),
             scientific = 5,
             digits = 3,
             p.levels.labels=c("^", "*","**","***"),
             "Table E4.rtf")


######################################################
##              Within subjects analysis            ##
######################################################
##Appendix H, Table H1

##Holding effect of Response constant across stage 1 treatment groups
#Feeling thermometer
study2.ft<- lm(ft_leader2 ~ ResponseCondition + ft_leader1,
               data=data2.partisans)
summary(study2.ft)

#Vote
study2.vote<- lm(voteL2 ~ ResponseCondition + voteL1,
                 data=data2.partisans)
summary(study2.vote)


#############################################
##              Power calc                 ##
#############################################
##Appendix I, Table I1

##study 2 - assembly leader ft
##Governor - Loyal
summary(data2.partisans$ft_critic[data2.partisans$ResponseCondition=="Agree"])
sd(data2.partisans$ft_critic[data2.partisans$ResponseCondition=="Agree"], na.rm=TRUE)
length(na.omit(data2.partisans$ft_critic[data2.partisans$ResponseCondition=="Agree"]))
##Governor - Dissent
summary(data2.partisans$ft_critic[data2.partisans$ResponseCondition=="Rebuke"])
sd(data2.partisans$ft_critic[ data2.partisans$ResponseCondition=="Rebuke"], na.rm=TRUE)
length(na.omit(data2.partisans$ft_critic[data2.partisans$ResponseCondition=="Rebuke"]))
##Power analysis
library(stats)
power.t.test(n=406, sig.level=0.05, power=0.80, sd=0.28)
##Note used lower of N in loyal and N in dissent for n, sd is larger sd in loyal and dissent
##could have detected a mean difference of 0.055 (observed difference was bigger and significant)
t.test(data2.partisans$ft_critic ~ data2.partisans$ResponseCondition)
0.4486967  - 0.6183005

##critic vote
##Governor - Loyal
summary(data2.partisans$vote_critic[data2.partisans$ResponseCondition=="Agree"])
sd(data2.partisans$vote_critic[data2.partisans$ResponseCondition=="Agree"], na.rm=TRUE)
length(na.omit(data2.partisans$vote_critic[data2.partisans$ResponseCondition=="Agree"]))
##Governor - Dissent
summary(data2.partisans$vote_critic[data2.partisans$ResponseCondition=="Rebuke"])
sd(data2.partisans$vote_critic[data2.partisans$ResponseCondition=="Rebuke"], na.rm=TRUE)
length(na.omit(data2.partisans$vote_critic[data2.partisans$ResponseCondition=="Rebuke"]))
##Power analysis
library(stats)
power.t.test(n=419, sig.level=0.05, power=0.80, sd=0.30)
##Note used lower of N in loyal and N in dissent for n, sd is larger sd in loyal and dissent
t.test(data2.partisans$vote_critic ~ data2.partisans$ResponseCondition)
0.4764802 - 0.6173062



